package com.bdqn.controller;

import com.alibaba.fastjson.JSONObject;
import com.bdqn.pojo.SmbmsBill;
import com.bdqn.pojo.SmbmsProvider;
import com.bdqn.serice.SmbmsBillService;
import com.bdqn.serice.SmbmsProviderService;
import com.mysql.cj.util.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author www
 * @Description: 控制器
 * @date 2020-06-01 11:19
 */
@Controller
public class BIllController {
    private Logger log=LogManager.getLogger(BIllController.class);
    @Resource
    private SmbmsProviderService smbmsProviderService;
    @Resource
    private SmbmsBillService smbmsBillService;
    @RequestMapping(value = "/queryList.do")
    public String queryList(
            @RequestParam(value="queryProductName",required = false) String queryProductName,
            @RequestParam(value="queryProviderId",required = false) String queryProviderId,
            @RequestParam(value="queryIsPayment",required = false) String queryIsPayment,Model model
    ){
        if(StringUtils.isNullOrEmpty(queryProductName)){
            queryProductName = "";
        }
        SmbmsBill smbmsBill=new SmbmsBill();
       // productName providerId isPayment
        if(StringUtils.isNullOrEmpty(queryIsPayment)){
            smbmsBill.setIsPayment(0);
        }else{
            smbmsBill.setIsPayment(Integer.parseInt(queryIsPayment));
        }
        if(StringUtils.isNullOrEmpty(queryProviderId)){
            smbmsBill.setProviderId(0);
        }else{
            smbmsBill.setProviderId(Integer.parseInt(queryProviderId));
        }
        smbmsBill.setProductName(queryProductName);
       List<SmbmsBill> list=smbmsBillService.selectBill(smbmsBill);

       //providerList
        model.addAttribute("providerList",smbmsProviderService.selectSmbmsProvider(null,null));
       model.addAttribute("billList",list);
        model.addAttribute("queryProductName",smbmsBill.getProductName());
        model.addAttribute("queryProviderId",smbmsBill.getProviderId());
        model.addAttribute("queryIsPayment",smbmsBill.getIsPayment());
        log.info("订单查询");
        return "billlist";
    }
    //新增 /addBill.do  /addBill.do
    @RequestMapping("/addBill.do")
    public String addBill(SmbmsBill smbmsBill){
        smbmsBill.setCreationDate(new Date());
        log.info("进入新增"+smbmsBill.getProductName());
        if(smbmsBill.getProductName() == null){
            log.info("点击新增跳转");
            return  "billadd";
        }
        int id=smbmsBillService.insertSelective(smbmsBill);
        if(id>0){
            return "redirect:queryList.do";
        }else{
            return  "billadd";
        }
    }
    //供应商下拉列表初始化
    @GetMapping(value = "/getproviderlist.do",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String getproviderlist() {
        log.info("初始化");
        List<SmbmsProvider> list=smbmsProviderService.selectSmbmsProvider(null,null);
        String jsonString=JSONObject.toJSONString(list);
        return jsonString;
    }
    //删除
    @GetMapping(value = "/billDel.do")
    @ResponseBody
    public String billDel(@RequestParam(value="billid",required = false) Long billid){
        log.info("进入删除");
        //deleteByPrimaryKey
        HashMap<String, String> resultMap = new HashMap<String, String>();


        int flag = smbmsBillService.deleteByPrimaryKey(billid);
        System.out.println(flag+"删除"+billid);
        if(billid>0){
            if(flag>0){//删除成功
                resultMap.put("delResult","true");
            }else{//删除失败
                resultMap.put("delResult","false");
            }
        }else{
            resultMap.put("delResult","notexist");
        }

            String jsonString=JSONObject.toJSONString(resultMap);
            return jsonString;

    }
    //查看 getBillById.do/billview/1
    @RequestMapping(value = "/getBillById.do/{url}/{uid}")
    public String getBillById(@PathVariable(value = "uid") String id, @PathVariable(value = "url") String url,Model model) {
        System.out.println(id+"id");
        SmbmsBill bill=smbmsBillService.selectByPrimaryKey(Long.parseLong(id));
        model.addAttribute("bill",bill);
        return url;
    }
    //修改
    @RequestMapping(value = "/billModify.do")
    public String billModify(SmbmsBill smbmsBill){
        log.info("进入修改");
        int id=smbmsBillService.updateByPrimaryKeySelective(smbmsBill);
        if(id>0){
            return "redirect:queryList.do";
        }else{
            return  "billmodify";
        }
    }

}
